home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 297_01 / exampl6.spr < prev    next >
Text File  |  1980-01-01  |  2KB  |  81 lines

  1. /* example6.spr */
  2. /* more on multiple rules */
  3. /* In prolog you can do without the or logical operator
  4.    and rely on multiple clauses for the same effect.
  5.    The logical and is taken care of by puting several
  6.    conditions to a rule.
  7.    The logical not is approximately covered by the 
  8.    "not" operator, but that's still too advanced.
  9.    The conditions of a rule become goals to solve
  10.    as prolog executes. The goals are tried from
  11.    left to right. Should all goals of a rule be solved
  12.    then the call to the rule succeeded.
  13.    This is not always the case.
  14.    For example in the following rule
  15. */
  16.  
  17. ((demo6)
  18.  
  19.  (display success_goal1)
  20.  (display success_goal2)
  21.  (display success_goal3)
  22.  (display success_goal4)
  23.  (fail)
  24.  (display success_goal5)
  25.  (display success_goal6)
  26.  (display success_goal7)
  27. )
  28.  
  29. /*
  30. Only the first 4 goals will succeed: fail is garanteed to fail.
  31. So will any goal that is neither builtin nor defined.
  32.  
  33. There is no alternative clause for demo6 so the execution ends there.
  34.  
  35. However in:
  36. */
  37. ((demo6_1)
  38.  
  39.  (display success_goal1)
  40.  (display success_goal2)
  41.  (display success_goal3)
  42.  (display success_goal4)
  43.  (fail)
  44.  (display success_goal5)
  45.  (display success_goal6)
  46.  (display success_goal7)
  47. )
  48.  
  49. ((demo6_1)
  50.  (display haha)
  51.  (nl)
  52. )
  53. /* 
  54. there is an alternative clause for demo6_1 and the execution
  55. backtracks and tries that. 
  56.  */
  57.  
  58. /* This idea is used in the following declarative program  
  59.  
  60. ((north_american Person)
  61.  (lives_in Person usa)
  62. )
  63. ((north_american Person)
  64.  (lives_in Person canada)
  65. )
  66. */
  67. /* which says that one is north american if one lives in USA or in 
  68.   Canada.
  69.  */
  70.  
  71. /* Incidentally a similar effect is obtained by:
  72. ((north_american Person)
  73.  (lives_in Person Country)
  74.  (member Country (usa canada))
  75. )
  76.  
  77. where member is defined in sprolog.ini.
  78. But that involves list-handling which is an advanced topic
  79. */
  80.  
  81.